//
//  AddPersonViewController.m
//  UI-08 HomeWork通讯录
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "AddPersonViewController.h"

@interface AddPersonViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic,strong) NSMutableDictionary *diction;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSString *gender;
@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic, strong) UIButton *button;
@end

@implementation AddPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationController.navigationBar.translucent = NO;
    for (int i = 0; i < 4; i++) {
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50 + (i * 80), 414 - 100, 50)];
        self.textField.backgroundColor = [UIColor colorWithRed:(arc4random() % 256) / 255.0 green:(arc4random() % 256) / 255.0 blue:(arc4random() % 256) / 255.0 alpha:1];
        [self.view addSubview:_textField];
        self.textField.tag = i;
        self.textField.delegate = self;
    }
    self.diction = [NSMutableDictionary dictionary];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(50, 370, 314, 50);
    [self.view addSubview:_button];
    _button.backgroundColor = [UIColor purpleColor];
    [_button setTitle:@"保存" forState:0];
    [_button addTarget:self action:@selector(restore) forControlEvents:UIControlEventTouchUpInside];
}
// 触发协议事件
- (void)restore {
    NSLog(@"%@",_diction);
    [self.delegate sendValue:_diction];
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == 0) {
        
        
        [_diction setObject:textField.text forKey:@"name"];
        NSLog(@"姓名%@,字典:%@",textField.text,_diction);
    } else if (textField.tag == 1) {
        
        [_diction setObject:textField.text forKey:@"age"];
    } else if (textField.tag == 2) {
        
        [_diction setObject:textField.text forKey:@"gender"];
    } else {
        
        [_diction setObject:textField.text forKey:@"phoneNumber"];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
